package cn.zwz.bus.controller;

import cn.zwz.basics.baseVo.PageVo;
import cn.zwz.basics.baseVo.Result;
import cn.zwz.basics.utils.PageUtil;
import cn.zwz.basics.utils.ResultUtil;
import cn.zwz.basics.utils.SecurityUtil;
import cn.zwz.bus.entity.BasicBusiness;
import cn.zwz.bus.entity.BasicBusinessItem;
import cn.zwz.bus.service.IBasicBusinessItemService;
import cn.zwz.bus.service.IBasicBusinessService;
import cn.zwz.data.entity.User;
import cn.zwz.data.service.IUserService;
import cn.zwz.data.utils.ZwzNullUtils;
import cn.zwz.data.vo.AntvVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author 郑为中
 * CSDN: Designer 小郑
 */
@Slf4j
@RestController
@Api(tags = "基础业务子项管理接口")
@RequestMapping("/zwz/basicBusinessItem")
@Transactional
public class BasicBusinessItemController {

    @Autowired
    private IBasicBusinessItemService iBasicBusinessItemService;

    @Autowired
    private IBasicBusinessService iBasicBusinessService;

    @Autowired
    private IUserService iUserService;

    @Autowired
    private SecurityUtil securityUtil;

    @RequestMapping(value = "/getOne", method = RequestMethod.GET)
    @ApiOperation(value = "查询单条基础业务子项")
    public Result<BasicBusinessItem> get(@RequestParam String id){
        return new ResultUtil<BasicBusinessItem>().setData(iBasicBusinessItemService.getById(id));
    }

    @RequestMapping(value = "/count", method = RequestMethod.GET)
    @ApiOperation(value = "查询全部基础业务子项个数")
    public Result<Long> getCount(){
        return new ResultUtil<Long>().setData(iBasicBusinessItemService.count());
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "查询全部基础业务子项")
    public Result<List<BasicBusinessItem>> getAll(){
        return new ResultUtil<List<BasicBusinessItem>>().setData(iBasicBusinessItemService.list());
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "查询基础业务子项")
    public Result<IPage<BasicBusinessItem>> getByPage(@ModelAttribute BasicBusinessItem basicBusinessItem ,@ModelAttribute PageVo page){
        QueryWrapper<BasicBusinessItem> qw = new QueryWrapper<>();
        User currUser = securityUtil.getCurrUser();
        QueryWrapper<User> userQw = new QueryWrapper<>();
        userQw.inSql("id","SELECT user_id FROM a_user_role WHERE del_flag = 0 AND role_id = '1536606659751841799'");
        userQw.eq("id",currUser.getId());
        long userCount = iUserService.count(userQw);
        if(userCount < 1L) {
            qw.eq("user_id",currUser.getId());
        }
        if(!ZwzNullUtils.isNull(basicBusinessItem.getTitle())) {
            qw.like("title",basicBusinessItem.getTitle());
        }
        if(!ZwzNullUtils.isNull(basicBusinessItem.getUserName())) {
            qw.like("user_name",basicBusinessItem.getUserName());
        }
        IPage<BasicBusinessItem> data = iBasicBusinessItemService.page(PageUtil.initMpPage(page),qw);
        return new ResultUtil<IPage<BasicBusinessItem>>().setData(data);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "增改基础业务子项")
    public Result<BasicBusinessItem> saveOrUpdate(BasicBusinessItem basicBusinessItem){
        if(iBasicBusinessItemService.saveOrUpdate(basicBusinessItem)){
            return new ResultUtil<BasicBusinessItem>().setData(basicBusinessItem);
        }
        return ResultUtil.error();
    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ApiOperation(value = "新增基础业务子项")
    public Result<BasicBusinessItem> insert(BasicBusinessItem basicBusinessItem){
        BasicBusiness b = iBasicBusinessService.getById(basicBusinessItem.getBusinessId());
        if(b == null) {
            return ResultUtil.error("业务不存在");
        }
        User currUser = securityUtil.getCurrUser();
        basicBusinessItem.setTitle(b.getTitle());
        basicBusinessItem.setContent(b.getContent());
        basicBusinessItem.setType(b.getType());
        basicBusinessItem.setPayStatus("未付款");
        basicBusinessItem.setAuditStatus("未审核");
        basicBusinessItem.setUserId(currUser.getId());
        basicBusinessItem.setUserName(currUser.getNickname());
        iBasicBusinessItemService.saveOrUpdate(basicBusinessItem);
        return new ResultUtil<BasicBusinessItem>().setData(basicBusinessItem);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation(value = "编辑基础业务子项")
    public Result<BasicBusinessItem> update(BasicBusinessItem basicBusinessItem){
        iBasicBusinessItemService.saveOrUpdate(basicBusinessItem);
        return new ResultUtil<BasicBusinessItem>().setData(basicBusinessItem);
    }

    @RequestMapping(value = "/delByIds", method = RequestMethod.POST)
    @ApiOperation(value = "删除基础业务子项")
    public Result<Object> delByIds(@RequestParam String[] ids){
        for(String id : ids){
            iBasicBusinessItemService.removeById(id);
        }
        return ResultUtil.success();
    }

    @RequestMapping(value = "/pay", method = RequestMethod.POST)
    @ApiOperation(value = "支付")
    public Result<Object> pay(@RequestParam String id){
        BasicBusinessItem item = iBasicBusinessItemService.getById(id);
        if(item == null) {
            return ResultUtil.error("业务单不存在");
        }
        item.setPayStatus("已付款");
        iBasicBusinessItemService.saveOrUpdate(item);
        return ResultUtil.success();
    }

    @RequestMapping(value = "/audit", method = RequestMethod.POST)
    @ApiOperation(value = "审核")
    public Result<Object> audit(@RequestParam String id){
        BasicBusinessItem item = iBasicBusinessItemService.getById(id);
        if(item == null) {
            return ResultUtil.error("业务单不存在");
        }
        item.setAuditStatus("已审核");
        iBasicBusinessItemService.saveOrUpdate(item);
        return ResultUtil.success();
    }

    @RequestMapping(value = "/getAntvVoList", method = RequestMethod.GET)
    @ApiOperation(value = "查询图表数据")
    public Result<List<AntvVo>> getAntvVoList(){
        List<AntvVo> ansList1 = new ArrayList<>();
        List<BasicBusinessItem> teacherList = iBasicBusinessItemService.list();
        for (BasicBusinessItem o : teacherList) {
            boolean flag = false;
            for (AntvVo vo : ansList1) {
                if(Objects.equals(vo.getTitle(),o.getTitle())) {
                    flag = true;
                    vo.setValue(vo.getValue().add(BigDecimal.ONE));
                    break;
                }
            }
            if(!flag) {
                AntvVo vo = new AntvVo();
                vo.setTitle(o.getTitle());
                vo.setType("申请个数");
                vo.setValue(BigDecimal.ONE);
                ansList1.add(vo);
            }
        }
        List<AntvVo> ansList2 = new ArrayList<>();
        for (BasicBusinessItem o : teacherList) {
            boolean flag = false;
            for (AntvVo vo : ansList2) {
                if(Objects.equals(vo.getTitle(),o.getTitle()) && Objects.equals("已付款",o.getPayStatus())) {
                    flag = true;
                    vo.setValue(vo.getValue().add(BigDecimal.ONE));
                    break;
                }
            }
            if(!flag) {
                AntvVo vo = new AntvVo();
                vo.setTitle(o.getTitle());
                vo.setType("付款个数");
                vo.setValue(BigDecimal.ONE);
                ansList2.add(vo);
            }
        }
        List<AntvVo> ansList3 = new ArrayList<>();
        for (BasicBusinessItem o : teacherList) {
            boolean flag = false;
            for (AntvVo vo : ansList3) {
                if(Objects.equals(vo.getTitle(),o.getTitle()) && Objects.equals("已审核",o.getAuditStatus())) {
                    flag = true;
                    vo.setValue(vo.getValue().add(BigDecimal.ONE));
                    break;
                }
            }
            if(!flag) {
                AntvVo vo = new AntvVo();
                vo.setTitle(o.getTitle());
                vo.setType("审核个数");
                vo.setValue(BigDecimal.ONE);
                ansList3.add(vo);
            }
        }
        ansList1.addAll(ansList2);
        ansList1.addAll(ansList3);
        return new ResultUtil<List<AntvVo>>().setData(ansList1);
    }
}
